﻿using System;
using nRoute.Components.Composition;
using nRoute.Utilities;

namespace nRoute.Mapping
{
    public class DefineResourceAttribute
        : MapResourceAttribute
    {
        private readonly Type _implementationType;

        public DefineResourceAttribute(Type resourceType)
            : this(resourceType, resourceType, null, null) { }

        public DefineResourceAttribute(Type resourceType, string name)
            : this(resourceType, resourceType, name, null) { }

        public DefineResourceAttribute(Type resourceType, Type lifetimeManagerType)
            : this(resourceType, resourceType, null, lifetimeManagerType) { }

        public DefineResourceAttribute(Type resourceType, string name, Type lifetimeManagerType)
            : this(resourceType, resourceType, name, lifetimeManagerType) { }

        //public DefineResourceAttribute(Type resourceType, Type implementationType)
        //    : this(resourceType, implementationType, null, null) { }

        public DefineResourceAttribute(Type resourceType, Type implementationType, string name)
            : this(resourceType, implementationType, name, null) { }

        public DefineResourceAttribute(Type resourceType, Type implementationType, Type lifetimeManagerType)
            : this(resourceType, implementationType, null, lifetimeManagerType) { }

        public DefineResourceAttribute(Type resourceType, Type implementationType, string name, Type lifetimeManagerType)
            : base(resourceType, name, lifetimeManagerType)
        {
            Guard.ArgumentNotNull(implementationType, "implementationType");
            EnsureIsResourceType(implementationType, resourceType);

            _implementationType = implementationType;
        }

        public virtual Type ImplementationType
        {
            get { return _implementationType; }
        }

#region Overrides

        protected internal override IResourceLocator GetResourceLocator(Type targetType, ITypeBuilder typeBuilder)
        {
            return base.GetResourceLocator(this.ImplementationType, typeBuilder);
        }

#endregion

    }
}
